package B2;
import java.util.Scanner;

public class PasswordControlPanel {

    private static final int MAX_ATTEMPTS = 3;
    private static final int LOCK_DURATION_SECONDS = 120;
    private static final int PASSWORD_LENGTH = 4;

    private String correctPassword;
    private String enteredPassword;
    private int attemptCount;
    private long lockStartTime;
    private boolean isLocked;

    public PasswordControlPanel(String correctPassword) {
        this.correctPassword = correctPassword;
        this.attemptCount = 0;
        this.isLocked = false;
    }

    public void enterCharacter(char character) {
        if (isLocked) {
            long currentTime = System.currentTimeMillis();
            long elapsedSeconds = (currentTime - lockStartTime) / 1000;
            if (elapsedSeconds >= LOCK_DURATION_SECONDS) {
                unlockPanel();
            } else {
                System.out.println("控制面板已锁定，请等待 " + (LOCK_DURATION_SECONDS - elapsedSeconds) + " 秒后再试。");
                return;
            }
        }

        if (enteredPassword == null) {
            enteredPassword = "";
        }
        enteredPassword += character;

        if (enteredPassword.length() == PASSWORD_LENGTH) {
            if (enteredPassword.equals(correctPassword)) {
                System.out.println("密码正确，控制面板进入系统功能选择状态。");
                // 这里可以添加进入系统功能选择状态的相关逻辑，比如调用相应的方法来展示功能菜单等
            } else {
                attemptCount++;
                if (attemptCount >= MAX_ATTEMPTS) {
                    lockPanel();
                } else {
                    System.out.println("密码错误，请重新输入。还有 " + (MAX_ATTEMPTS - attemptCount) + " 次机会。");
                    enteredPassword = "";
                }
            }
        }
    }

    private void lockPanel() {
        isLocked = true;
        lockStartTime = System.currentTimeMillis();
        System.out.println("密码错误次数过多，控制面板已锁定，请等待 " + LOCK_DURATION_SECONDS + " 秒后再试。");
    }

    private void unlockPanel() {
        isLocked = false;
        attemptCount = 0;
        enteredPassword = "";
        System.out.println("控制面板已解锁，可重新输入密码。");
    }

    public static void main(String[] args) {
        String correctPassword = "1234"; // 这里假设正确密码是1234，实际应用中可通过配置等方式设置
        PasswordControlPanel panel = new PasswordControlPanel(correctPassword);

        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入密码，每次输入一个字符：");

        while (true) {
            char inputChar = scanner.next().charAt(0);
            panel.enterCharacter(inputChar);

            if (!panel.isLocked && panel.enteredPassword!= null && panel.enteredPassword.equals(panel.correctPassword)) {
                break;
            }
        }

        scanner.close();
    }
}